package com.yikaiye.android.yikaiye.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.databinding.ItemServiceProgressBinding;
import com.yikaiye.android.yikaiye.model.ServiceDetailsModel;

public class NodeProgressAdapter extends BaseQuickAdapter<ServiceDetailsModel.ServiceNodeListModel, DataBindingHolder<ItemServiceProgressBinding>> {

    private final Context context;
    private int mStatus;

    public NodeProgressAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemServiceProgressBinding> itemMyServiceBindingDataBindingHolder, int i, @Nullable ServiceDetailsModel.ServiceNodeListModel serviceNodeListModel) {
        ItemServiceProgressBinding binding = itemMyServiceBindingDataBindingHolder.getBinding();
        if (serviceNodeListModel != null) {
            if (getItemCount() == 3) {
                if (mStatus == 1) {
                    if (i == 0) {
                        binding.serviceStatusTv.setText(context.getString(R.string.service_start));
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_3b3a40));
                        binding.serviceStatusTv.setTypeface(Typeface.DEFAULT);
                        binding.statusIv.setImageResource(R.mipmap.img_v);
                    } else if (i == 1) {
                        binding.serviceStatusTv.setText(context.getString(R.string.in_service));
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_f93f50));
                        binding.serviceStatusTv.setTypeface(Typeface.DEFAULT_BOLD);
                        binding.statusIv.setImageResource(R.mipmap.img_v_zhong);
                        InServiceStepAdapter adapter = new InServiceStepAdapter(context, serviceNodeListModel.getStatus());
                        binding.rv.setAdapter(adapter);
                        adapter.submitList(serviceNodeListModel.getNodeList());
                    } else if (i == 2) {
                        binding.serviceStatusTv.setText(context.getString(R.string.service_finish));
                        binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_9b9b9b));
                        binding.serviceStatusTv.setTypeface(Typeface.DEFAULT);
                        binding.statusIv.setImageResource(R.mipmap.img_def);
                    }
                } else {
                    binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, R.color.color_3b3a40));
                    binding.statusIv.setImageResource(R.mipmap.img_v);
                    binding.serviceStatusTv.setTypeface(Typeface.DEFAULT);
                    if (i == 0) {
                        binding.serviceStatusTv.setText(context.getString(R.string.service_start));
                    } else if (i == 1) {
                        binding.serviceStatusTv.setText(context.getString(R.string.in_service));
                        InServiceStepAdapter adapter = new InServiceStepAdapter(context, serviceNodeListModel.getStatus());
                        binding.rv.setAdapter(adapter);
                        adapter.submitList(serviceNodeListModel.getNodeList());
                    } else if (i == 2) {
                        binding.serviceStatusTv.setText(context.getString(R.string.service_finish));
                    }
                }
            }
            binding.timeTv.setText(serviceNodeListModel.getAssignTime());

            if (serviceNodeListModel.getNodeList() != null && !serviceNodeListModel.getNodeList().isEmpty()) {
                binding.line.setVisibility(View.GONE);
            } else {
                binding.line.setVisibility(i == getItemCount() - 1 ? View.GONE : View.VISIBLE);
            }
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemServiceProgressBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_service_progress, viewGroup);
    }

    public void setStatus(int status) {
        mStatus = status;
    }
}
